package zhihui.mobile.ui.welcome;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.google.gson.Gson;

import org.json.JSONObject;

import zhihui.mobile.R;
import zhihui.mobile.app.MyApplication;
import zhihui.mobile.bean.token.TokenInfo;
import zhihui.mobile.config.Config;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.token.TokenManager;
import zhihui.mobile.ui.main.MainActivity;

/**
 * Created by 张公铭 on 2017/6/19 0019.
 * 欢迎界面
 */

public class WelcomeActivity extends AppCompatActivity
{
    private ImageView iv_log;
    private AlphaAnimation alphaAnimation;
    private boolean showGuidance = true;    //是否显示引导页，true 显示


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome_activity);
        initView();
        initToken();
    }

    private void initToken()
    {
        new Thread()
        {
            @Override
            public void run()
            {
                TokenManager.getInstance().getToken();
            }
        }.start();
    }

    private void initView()
    {
        iv_log = (ImageView) findViewById(R.id.iv_welcome_log);
        alphaAnimation = (AlphaAnimation) AnimationUtils.loadAnimation(this, R.anim.welcom_log_anima);
        alphaAnimation.setAnimationListener(new Animation.AnimationListener()
        {
            @Override
            public void onAnimationStart(Animation animation)
            {

            }

            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onAnimationEnd(Animation animation)
            {
                if (showGuidance)
                {
                    Intent intent = new Intent(WelcomeActivity.this, GuidanceActivity.class);
                    startActivity(intent);
                    MyApplication.finishActivity(WelcomeActivity.this);
                } else
                {
                    Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                    startActivity(intent);
                    MyApplication.finishActivity(WelcomeActivity.this);
                }
                finish();
            }

            @Override
            public void onAnimationRepeat(Animation animation)
            {

            }
        });

        SharedPreferences preferences = this.getSharedPreferences(Config.GUIDANCE_STATE, Activity.MODE_PRIVATE);
        if (preferences != null)
        {
            showGuidance = preferences.getBoolean(Config.GUIDANCE_STATE_KEY, true);
        }
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        if (android.os.Build.VERSION.SDK_INT < 23)
        {

        }
        iv_log.startAnimation(alphaAnimation);
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        iv_log.clearAnimation();
        alphaAnimation.cancel();
    }
}
